import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter)

import routes from "routes/routes"
const router = new VueRouter({
  routes,
  mode:"history"
})

//处理重复点击编程式路由出错的问题
const VueRouterPush = VueRouter.prototype.push;
const VueRouterReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (to){
  return VueRouterPush.call(this,to).catch(err => err)
}
VueRouter.prototype.replace = function (to){
  return VueRouterReplace.call(this,to).catch(err => err)
}

//全局前置守卫
/*router.beforeEach((to, from, next)=>{
  //不调next       //不放行
  //next(false)   //不放行
  //next(new Error("给钱"))   //不放行且报错

  //next(path) next({path:""})  //放行到指定的路由
  // if(to.path==="/home"){
  //   next("/about")
  // }else{
  //   next()
  // }

  console.log("全局前置守卫");
  next()        //放行
})*/

//全局解析守卫
/*router.beforeResolve(async (to, from, next)=>{
  await new Promise((resolve, reject)=>{
    setTimeout(()=>{
      console.log("全局解析守卫");
      resolve()
    },2000)
  })
  next()        //放行
})*/

//全局后置守卫
/*router.afterEach((to, from) => {
  console.log("全局后置守卫")
})*/

export default router
